package com.graduate.cec.controller;

import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.JsonResult;
import com.graduate.cec.pojo.dto.LoginDTO;
import com.graduate.cec.pojo.dto.RegisterDTO;
import com.graduate.cec.pojo.dto.UserSimpleDTO;
import com.graduate.cec.pojo.entity.User;
import com.graduate.cec.pojo.vo.UserInfoVO;
import com.graduate.cec.pojo.vo.UserSimpleVO;
import com.graduate.cec.service.LoginService;
import com.graduate.cec.service.UserRoleService;
import com.graduate.cec.service.UserService;
import com.graduate.cec.util.CopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping(value = "/user",produces = "application/json;charset=utf-8")
@Api(tags = "用户")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private LoginService login;
    /****************        增       ******************************************************************/
    @PostMapping("/user-register")
    public JsonResult<String> insertRegisterUser(@RequestBody RegisterDTO registerDTO) {
        Long register = userService.register(registerDTO);

        userRoleService.insertRegister(register);

        LoginDTO copy = CopyUtil.copy(registerDTO, LoginDTO.class);

        String jwt = login.login(copy, "普通用户");

        return JsonResult.ok("注册成功",jwt);
    }

    //上传图片
    @PostMapping("/upload-picture")
    @ApiOperation("上传图片")
    public JsonResult<String> upload(MultipartFile picFile) throws Exception {
        String fileName = userService.uploadPictures(picFile);
        return JsonResult.ok("插入成功",fileName);

    }

    //上传图片
    @PostMapping("/update-avatar")
    @ApiOperation("上传图片")
    public JsonResult<UserSimpleVO> updateAvatar(@RequestBody User user) {
        UserSimpleVO userSimpleVO = userService.updateAvatar(user);
        return JsonResult.ok("插入成功",userSimpleVO);

    }


    /****************        删       ******************************************************************/
    //删除上传的图片
    @GetMapping("/remove-picture")
    @ApiOperation("删除图片")
    public JsonResult<Void> remove(String pictureName) {
        userService.removePicture(pictureName);
        return JsonResult.ok("删除成功");
    }


    /****************        改       ******************************************************************/


    @PostMapping("/update-info")
    @ApiOperation("修改用户信息")
    public JsonResult<Void> updateInfo(@RequestBody User user) {
        userService.updateInfo(user);
        return JsonResult.ok("修改成功");
    }
    /****************        查        ******************************************************************/




    /**
     *
     * @param loginDTO 登录
     * @return String
     */


    @PostMapping("/login")
    @ApiOperation("查询全部列表")
    public JsonResult<String> login(@RequestBody LoginDTO loginDTO){
        log.info("userLoginDTO:{}", loginDTO);
        return JsonResult.ok("查询成功",login.login(loginDTO,"普通用户"));
    }


    @GetMapping("/personal")
    @ApiOperation("根据电话查询信息")
    public JsonResult<UserSimpleVO> selectSimplePersonal() {
        return JsonResult.ok("查询成功",userService.selectSimplePersonal());
    }

    @GetMapping("/info")
    @ApiOperation("根据电话查询基本信息")
    public JsonResult<UserInfoVO> selectInfoByPhone() {
        return JsonResult.ok("查询成功",userService.selectInfoByPhone());
    }

    @GetMapping("/get-list-by-example")
    @ApiOperation("通过一定条件查找列表")
    public JsonResult<JsonPage<User>>getListByExample(UserSimpleDTO userSimpleDTO){
        return JsonResult.ok("查询成功",userService.getListByExample(userSimpleDTO));
    }


}
